local base = game_mode.netbase
local survive = class("survive_mode",base)

survive.roomMax = 20
survive.roomMin = 1
survive.roomLast = 600
survive.mapName = "random"
survive.mapW = 100
survive.mapH = 100
survive.teamCount = 20
survive.itemGenCD = 30
survive.itemGenTypes = {1,2,3,4,5,6,7}
survive.npcGenCD = 5
survive.npcGenTypes = {1,2,3,4,5,6,7,8}
survive.npcGenCollCheck = false
survive.enablePlayerRebirth = false
survive.playerRebirthCD = 1
survive.playerBirthCollCheck = false
survive.playerGenTypes = {1,2,3,4,5,6,7,8}

function survive:onInit()
    self.waterlevel = 0
    self.watertimer = 20
    self.waterCD = 20
end

function survive:getPlayerBirthPos(player)
    return {
        minX = 1+self.waterlevel,
        maxX = self.map.w-1 - self.waterlevel,
        minY = 1+self.waterlevel,
        maxY = self.map.h-1 - self.waterlevel
        }
end

function survive:getNPCGenPos()
    return {
        minX = 1+self.waterlevel,
        maxX = self.map.w-1 - self.waterlevel,
        minY = 1+self.waterlevel,
        maxY = self.map.h-1 - self.waterlevel
        }
end


function survive:onPlayerDead(player)
    self:removePlayer()
    player.peer:send("game_over",
        {["败北"] = "你生存了"..math.ceil(survive.roomLast - self.gameTimer).."秒"})
    if self.playerCount == 0 then
        net:destroyRoom(self.room)
    end
end

function survive:onTimeUp()
    net:sendToRoom(self.room,"game_over", 
        {["胜利"] = "大吉大利，今晚吃鸡"})
    net:destroyRoom(self.room)
end


function survive:onUpdate(dt)
    self.watertimer = self.watertimer - dt
	if self.watertimer<0 then
		self.watertimer = self.waterCD
		self.waterlevel = self.waterlevel + 1
        for x = self.waterlevel,self.map.w -self.waterlevel + 1 do
            self.map:setTile(x,self.waterlevel,4)
            self.map:setTile(x,self.map.h - self.waterlevel + 1,4)
        end
        for y = self.waterlevel,self.map.h-self.waterlevel + 1 do
            self.map:setTile(self.waterlevel,y,4)
            self.map:setTile(self.map.w - self.waterlevel + 1,y,4)
        end
        for id,obj in pairs(self.objects) do
            if obj.tag =="tank" then 
                if obj.x<self.waterlevel*16 
                    or obj.x>(self.map.w - self.waterlevel + 1)*16 
                    or obj.y<self.waterlevel*16 
                    or obj.y>(self.map.h - self.waterlevel + 1)*16 then
                    obj:destroy()
                    if obj.player then
                        self:onPlayerDead(obj.player)
                    end
                end
            end
        end
	end
end

return survive